/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.common;

import hibernate.dao.AccountDAO;
import hibernate.entity.Accounts;
import hibernate.entity.Staffs;
import hibernate.entity.Students;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class Login extends AbstractUIBean {

    /**
     * Creates a new instance of Login
     */
    String username;
    String password;
    int roleID;
    AccountDAO accDao = new AccountDAO();
    boolean isRemember, isError;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isIsRemember() {
        return isRemember;
    }

    public void setIsRemember(boolean isRemember) {
        this.isRemember = isRemember;
    }

    public boolean isIsError() {
        return isError;
    }

    public void setIsError(boolean isError) {
        this.isError = isError;
    }

    public int getRoleID() {
        return roleID;
    }

    public void setRoleID(int roleID) {
        this.roleID = roleID;
    }

    /**
     * @return String
     * @see admin's index or staffprofile or studentprofile page
     */
    public String dologin() {
        hibernate.entity.Accounts acc = accDao.login(username, password);
        if (acc != null) {
            if (isRemember) {
                HttpServletRequest request = getRequest();
                HttpServletResponse respond = getRespond();
                Cookie ckRemember = new Cookie("currAccID", String.valueOf(acc.getAccountId()));

            }
            roleID = acc.getRoleId();
            setCurrentAccount(acc);
            setCurrentRole(roleID);

            if (acc.getStatus() == 0) {
                return "userchangepass";
            }
            if (roleID == 1) {
                return "admin-index";
            }
            if (roleID == 2) {
                HttpSession session = getHttpSession();
                Staffs s = acc.getStaffses().iterator().next();
                session.setAttribute("currStaff", s);
                return "staffprofile";
            }
            if (roleID == 3) {
                HttpSession session = getHttpSession();
                Students s = acc.getStudentses().iterator().next();
                session.setAttribute("currStudent", s);
                return "studentprofile";
            }

        }
        return null;

    }

    /**
     * @return String
     * @see changepass page
     */
    public String changepass() {
        String path = request.getContextPath();
        return path + "/personal/changepass";
    }

    /**
     * @return String
     * @see logout page
     */
    public String logout() {
        HttpSession session = getHttpSession();
        if (session.getAttribute("currAcc") != null) {
            session.invalidate();
            return "userlogout";
        }
        return null;



    }

    /**
     * @return String
     * @see profile
     */
    public String profile() {
        Accounts a = getCurrentAccount();
        roleID = getCurrentRole();
        switch (roleID) {
            case 2:
                return "staffprofile";
            case 3:
                return "studentprofile";
        }
        return null;
    }

    /**
     * @return String
     * @see forgot page
     */
    public String forgot() {
        return "forgot";
    }

    public Login() {
    }
}
